home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / src / testcfft.cc < prev    next >
C/C++ Source or Header  |  1989-08-18  |  4KB  |  138 lines

  1. /*
  2.  *    Test Complex FFT server
  3.  *
  4.  *    Copyright (C) 1988, 1989.
  5.  *
  6.  *    Dr. Thomas Keffer
  7.  *    Rogue Wave Associates
  8.  *    P.O. Box 85341
  9.  *    Seattle WA 98145-1341
  10.  *
  11.  *    Permission to use, copy, modify, and distribute this
  12.  *    software and its documentation for any purpose and
  13.  *    without fee is hereby granted, provided that the
  14.  *    above copyright notice appear in all copies and that
  15.  *    both that copyright notice and this permission notice
  16.  *    appear in supporting documentation.
  17.  *    
  18.  *    This software is provided "as is" without any
  19.  *    expressed or implied warranty.
  20.  *
  21.  *
  22.  *    @(#)testcfft.cc    2.1    8/18/89
  23.  */
  24.  
  25.  
  26. #include "rw/DComplexFFT.h"
  27.  
  28. overload printVec;
  29.  
  30. static void
  31. printVec(ostream& s, const DComplexVec& x);
  32.  
  33. static void
  34. printVec(ostream& s, const DoubleVec& x);
  35.  
  36. main()
  37. {
  38.   const unsigned npts = 12;
  39.   cout <<"Testing DComplexFFTServer (Double Precision Complex FFT Server)\n\n";
  40.   
  41.   DComplexFFTServer     server;
  42.  
  43.   // Isn't this stuff wonderful?!
  44.   DComplexVec a(cos(DoubleVec(npts, 0, 2.0*M_PI/npts))); // One cycle
  45.   DComplexVec a2(sin(DoubleVec(npts, 0, 4.0*M_PI/npts)));// Two cycles
  46.   DComplexVec asum = a + a2;                 // Superposition
  47.  
  48.   cout <<"**************************************\n";
  49.   cout <<"a:\n";
  50.   printVec(cout,a);
  51.   cout <<"a2:\n";
  52.   printVec(cout,a2);
  53.   cout <<"asum:\n";
  54.   printVec(cout, asum);
  55.  
  56.   cout <<"**************************************\n";
  57.   cout <<"Checking transforms of pure signals.\n";
  58.   cout <<"\nTransform of a:\n";
  59.   printVec(cout,server.fourier(a)/DComplex(npts));
  60.  
  61.   cout <<"\nTransform of a2:\n";
  62.   printVec(cout, server.fourier(a2)/DComplex(npts));
  63.  
  64.   cout <<"\nTransform of asum:\n";
  65.   DComplexVec asum_fourier = server.fourier(asum)/DComplex(npts);
  66.   printVec(cout, asum_fourier);
  67.   
  68.   cout <<"**************************************\n";
  69.  
  70.   cout <<"Checking Parseval's theorem.\n";
  71.   cout <<"\nOriginal variance: "<<variance(asum)<<NL;
  72.  
  73.   double var = spectralVariance(asum_fourier);
  74.   cout <<"Spectral variance: "<<var<<"\n\n";
  75.  
  76.   cout <<"**************************************\n";
  77.   
  78.   DComplexVec aramp(npts,0,DComplex(1,1));
  79.   cout <<"Checking transform of linear ramp.\n";
  80.   cout <<"\nOriginal series:\n";
  81.   printVec(cout, aramp);
  82.  
  83.   DComplexVec aramp_fourier = server.fourier(aramp)/DComplex(npts);
  84.   cout <<"Its transform:\n";
  85.   printVec(cout, aramp_fourier);
  86.   cout <<NL;
  87.   cout <<"Checking Parseval's theorem for ramp.\n";
  88.   cout <<"Original variance: "<<variance(aramp)<<NL;
  89.  
  90.   var = spectralVariance(aramp_fourier);
  91.   cout <<"Final variance: "<<var<<NL;
  92.  
  93.   cout <<"\nBack transform:\n";
  94.   printVec(cout, server.ifourier(aramp_fourier));
  95.  
  96.   cout <<"**************************************\n";
  97.   cout <<"Checking Nyquist.\n";
  98.   DComplexVec Nyquist(npts,DComplex(1.0,1.0));
  99.   Nyquist.slice(1,npts/2,2) = DComplex(-1.0, -1.0);
  100.   cout <<"Original sequence:\n";
  101.   printVec(cout, Nyquist);
  102.   cout <<"Its transform:\n";
  103.   DComplexVec Nyquist_fourier = server.fourier(Nyquist)/DComplex(npts);
  104.   printVec(cout, Nyquist_fourier);
  105.   cout <<"Back transform:\n";
  106.   printVec(cout, server.ifourier(Nyquist_fourier));
  107.   
  108.   cout <<"**************************************\n";
  109.   exit(0);
  110. }
  111.  
  112. // Use special functions to print vectors, to round the output
  113. // to a fixed point number, so that differences at the machine
  114. // precision won't be seen.
  115.  
  116. static void
  117. printVec(ostream& s, const DComplexVec& x)
  118. {
  119.   for(int i = 0; i<x.length(); i++){
  120.     double rl = real(x(i));
  121.     double img = imag(x(i));
  122.     if(!(i%4) && i) s<<"\n";
  123.     s << "(" << form("%8.5f", rl) << ", ";
  124.     s << form("%8.5f", img) << ")" ;
  125.   }
  126.   s<<"\n";
  127. }
  128.  
  129. static void
  130. printVec(ostream& s, const DoubleVec& x)
  131. {
  132.   for(int i = 0; i<x.length(); i++){
  133.     if(!(i%5) && i) s<<"\n";
  134.     s << form("%8.5f",x(i)) << " ";
  135.   }
  136.   s<<"\n";
  137. }
  138.